<template>
  <div class="page">
    <!-- 搜索区域 开始 -->
    <div class="el_card_box">
      <el-form
        :inline="true"
        size="small"
        :model="formInline"
        class="search_form"
      >
        <el-form-item label="活动名称：">
          <!-- <user-select v-model="formInline.principals"></user-select> -->
          <el-select v-model="formInline.activityIds" filterable clearable>
            <el-option
              v-for="(item, index) in activiList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="来源：">
          <user-source v-model="formInline.channelSources"></user-source>
        </el-form-item>
        <el-form-item label="广告代号：">
          <adcode-select v-model="formInline.adSources"></adcode-select>
        </el-form-item>
        <el-form-item label="创建人：">
          <user-select v-model="formInline.principals"></user-select>
        </el-form-item>
        <el-form-item label="报名时间：">
          <el-date-picker
            v-model="formInline.orderTime"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="关键词：">
          <el-input
            v-model="formInline.term"
            placeholder="请输入客户电话/姓名"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="search_btn_box">
        <el-button type="primary" size="small" @click="getList(1)"
          >筛选</el-button
        >
        <el-button type="default" size="small" @click="rest"
          >清空筛选</el-button
        >
      </div>
    </div>
    <!-- 搜索区域 end -->

    <!-- 表格 开始 -->
    <div class="table_box">
      <!-- 操作区域 开始 -->
      <div class="action_box">
        <el-button
          type="default"
          size="small"
          :loading="eloading"
          @click="downloadExcel()"
          >筛选批量导出</el-button
        >
      </div>
      <!-- 操作区域 end -->

      <el-table
        ref="elTable"
        class="el_table"
        size="small"
        :data="tableData"
        @sort-change="sortChange"
      >
        <el-table-column
          prop="activityName"
          label="活动名称"
          show-overflow-tooltip
        />
        <el-table-column prop="name" label="姓名" show-overflow-tooltip />
        <el-table-column
          prop="phone"
          label="手机"
          :width="tdWidth"
          show-overflow-tooltip
        />
        <el-table-column
          prop="dentalProblems"
          label="牙齿问题"
          :width="tdWidth"
          show-overflow-tooltip
        />
        <el-table-column
          prop="intentProjects"
          label="想要参与活动"
          :width="tdWidth"
          show-overflow-tooltip
        />
        <el-table-column prop="city" label="所在城市" show-overflow-tooltip />
        <el-table-column prop="gender" label="性别" show-overflow-tooltip />
        <el-table-column prop="age" label="年龄" show-overflow-tooltip />
        <el-table-column prop="wxAccount" label="微信" show-overflow-tooltip />
        <el-table-column
          prop="channelSource"
          label="来源"
          show-overflow-tooltip
        />
        <el-table-column
          prop="adSource"
          label="广告代号"
          show-overflow-tooltip
        />
        <el-table-column
          prop="activityCity"
          label="活动城市"
          :width="tdWidth"
          show-overflow-tooltip
        />
        <el-table-column
          prop="enrollTime"
          label="报名时间"
          :width="tdWidth"
          show-overflow-tooltip
          sortable="orderDirection"
        />
        <el-table-column
          prop="ip"
          label="ip"
          :width="tdWidth"
          show-overflow-tooltip
        />
        <el-table-column
          prop="ipBelong"
          label="ip所在城市"
          :width="tdWidth"
          show-overflow-tooltip
        />
        <el-table-column
          prop="address"
          label="操作"
          width="120"
          align="center"
          fixed="right"
        >
          <template slot-scope="{ row }">
            <el-link type="primary" class="el_link" @click="openEdit(row)"
              >编辑</el-link
            >
            <el-link type="danger" class="el_link" @click="remove(row)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination_list">
        <el-pagination
          background
          layout="sizes, total,prev, pager, next, jumper"
          :total="pageTotal"
          :page-size="pageSize"
          :page-sizes="pageSizes"
          :current-page.sync="pageNumber"
          @current-change="getList"
          @size-change="changePageSize"
        />
      </div>
    </div>
    <!-- 表格 end -->

    <!-- 编辑线索弹框 开始 -->
    <edit-clue v-model="showDialog" :id="clueId" @change="getList"></edit-clue>
    <!-- 编辑线索弹框 end -->
    
  </div>
</template>

<script>
import { showModal, showToast, deepClone } from "@/common/util";
import {
  GET_ACTIVITY_OPTIONS,
  GET_CLUE_OPTIONS,
  DOWNLOAD_ACTIVITY_CLUE_EXPORT,
  DELETE_ACTIVITY_CLUE_INFO,
} from "@/http/clues";
import EditClue from "./edit.vue";
export default {
  components: {
    EditClue,
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      pageTotal: 0,
      pageNumber: 1,
      pageSizes: [10, 20, 50, 100],
      pageSize: 20,
      loading: false,
      eloading: false,
      activiList: [],
      tdWidth: 170,
      showDialog: false,
      clueId:''
    };
  },
  created() {
    this.getAllActivity();
    this.getList();
  },
  mounted() {},
  methods: {
    /** 获取用户列表 */
    getList(current) {
      let _this = this;
      let oParams = _this.getParams();
      oParams.pageNumber = current || _this.pageNumber;
      _this.loading = true;
      GET_CLUE_OPTIONS(oParams).then((res) => {
        _this.loading = false;
        let obj = res.data || {};
        _this.tableData = obj.list || [];
        _this.pageTotal = obj.totalElements;
      });
    },
    /** 每页展示 */
    changePageSize(e) {
      this.pageSize = e;
      this.getList();
    },

    /** 获取参数 */
    getParams() {
      let o = JSON.parse(JSON.stringify(this.formInline));
      let m = o.orderTime || [];
      if (m && m.length > 0) {
        o.enrollStartTime = m[0];
        o.enrollEndTime = m[1];
      }

      delete o.orderTime;
      o.pageSize = this.pageSize;
      return o;
    },

    /** 重置 */
    rest() {
      this.formInline = {};
      this.pageNumber = 1;
      this.getList();
    },

    // 导出excel
    downloadExcel() {
      let _this = this;
      let o = deepClone(_this.formInline);
      let m = Object.keys(o);
      if (m.length > 0) {
        showModal({
          message: "确定是否导出筛选条件下面所有线索列表?",
          showCancelButton: true,
          type: "warning",
          success: function () {
            _this.dowloadExcelFile();
          },
        });
        return false;
      }

      _this.dowloadExcelFile();
    },

    // 下载excel
    dowloadExcelFile() {
      let _this = this;
      let oParams = _this.getParams();
      let options = {};
      delete oParams.pageNumber;
      delete oParams.pageSize;

      options.method = "GET";
      options.data = oParams;
      options.url = DOWNLOAD_ACTIVITY_CLUE_EXPORT;
      options.fileName = "线索列表.xlsx";
      _this.eloading = true;
      _this.$exportExcel(options).then((res) => {
        _this.eloading = false;
      });
    },

    // 删除
    remove(row) {
      let _this = this;
      showModal({
        message: "确认删除该条数据吗？",
        type: "warning",
        showCancelButton: true,
        success: function () {
          DELETE_ACTIVITY_CLUE_INFO({ id: row.id }).then((res) => {
            if (res.code == 200) {
              showToast({
                message: "删除成功",
                type: "success",
              });
              _this.getList();
            }
          });
        },
      });
    },

    // 获取所有的活动
    getAllActivity() {
      GET_ACTIVITY_OPTIONS().then((res) => {
        this.activiList = res.data || [];
      });
    },

    // 监听表格排序
    sortChange({ column, prop, order }) {
      let o = { orderField: prop };
      this.formInline.orderField = prop;
      if (order == "ascending") {
        this.formInline.orderDirection = "ASC";
      } else {
        this.formInline.orderDirection = "DESC";
      }
      this.getList();
    },

    // 打开编辑线索弹框
    openEdit(row){
      this.showDialog = true;
      this.clueId = row.id
    }

  },
};
</script>

<style lang="scss" scoped>
</style>
